998B - Cutting - CodeForces Solution


dp greedy sortings *1200

Please click on ads to support us..

Python Code:

def cutting(n, b, array):
    odd, even = 0, 0
    for val in array:
        if val % 2 == 0: even += 1
        else: odd += 1
    
    new_odd, new_even = 0, 0
    if array[0] % 2 == 0: new_even += 1; even -= 1
    else: new_odd += 1; odd -= 1
            
    cost = []
    for i in range(1, len(array) - 1):
        if array[i] % 2 == 0: new_even += 1; even -= 1
        else: new_odd += 1; odd -= 1
        if odd == even and new_odd == new_even:
            cost.append(abs(array[i+1]-array[i]))
    cost.sort()
    cuts = 0
    for c in cost:
        if c>b: break
        b-=c
        cuts+=1
    print(cuts)


n, b = input().split(' ')
n, b = int(n), int(b)
array = input().split(' ')
array = [int(val) for val in array]
cutting(n, b, array)

C++ Code:

#include <bits/stdc++.h>

using namespace std;

int main () {
	int n, b;
	cin >> n >> b;
	int a[n];
	for (int i = 0; i < n; i++){
		cin >> a[i];
	}
	int zj = 0, fr = 0;
	vector <int> ekh;
	for (int i = 0; i < n; i++) {
		if (a[i] % 2 == 0) zj ++;
		if (a[i] % 2 == 1) fr ++;
		if (zj == fr && i != n - 1) {
			ekh.push_back(abs(a[i] - a[i + 1]));
		}
	}
	int ans = 0;
	sort (ekh.begin(), ekh.end());
	for (int i = 0; i < ekh.size(); i++){
		if(b >= 0 && ekh[i] <= b){
			b -= ekh[i];
			ans++;
		}
	}
	cout << ans;
}
    		 		 	 	 	  		  		 	 	 			


Comments

Submit
0 Comments
More Questions

1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes
1005B - Delete from the Left
94A - Restoring Password
1529B - Sifid and Strange Subsequences
1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments
1428B - Belted Rooms
519B - A and B and Compilation Errors
1152B - Neko Performs Cat Furrier Transform
1411A - In-game Chat
119A - Epic Game
703A - Mishka and Game